#!/bin/bash
# Run dedup benchmark

###SHELLPACK preamble dedup-bench 0

ITERATIONS=12
TESTLIST=

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam	--min-threads	DEDUP_MIN_THREADS
###SHELLPACK parseargParam	--max-threads	DEDUP_MAX_THREADS
###SHELLPACK parseargParam	--iterations	DEDUP_ITERATIONS
###SHELLPACK parseargParam	--file          DEDUP_FILE
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required dedup-${VERSION}
###SHELLPACK init_complete

# Copy/download test file into the test partition
if [ "$DEDUP_FILE" = "" ]; then
	die DEDUP_FILE must be specified
fi
if [ -e $DEDUP_FILE ]; then
	mmtests_activity Copying
	cp $DEDUP_FILE $SHELLPACK_TEMP/in.file.gz
	if [ $? -ne 0 ]; then
		die Failed to copy $DEDUP_FILE
	fi
else
	mmtests_activity Downloading
	echo Downloading $DEDUP_FILE
	wget -O $SHELLPACK_TEMP/in.file.gz -q $DEDUP_FILE
	if [ $? -ne 0 ]; then
		wget -O $SHELLPACK_TEMP/in.file.gz $DEDUP_FILE
		if [ $? -ne 0 ]; then
			die Failed to download $DEDUP_FILE
		fi
	fi
fi
if [ "`file $SHELLPACK_TEMP/in.file.gz | grep gzip`" = "" ]; then
	echo Source file already uncompressed
	mv $SHELLPACK_TEMP/in.file.gz $SHELLPACK_DATA/in.file
else
	echo Uncompressing source file
	gzip -dc $SHELLPACK_TEMP/in.file.gz >$SHELLPACK_DATA/in.file || die Failed to decompress source file
fi

###SHELLPACK threads_large_stride_begin $DEDUP_MIN_THREADS $DEDUP_MAX_THREADS $DEDUP_ITERATIONS
	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
	###SHELLPACK iteration_begin $DEDUP_ITERATIONS
		echo Starting threads $NR_THREADS/$DEDUP_MAX_THREADS iteration $ITERATION/$DEDUP_ITERATIONS
		save_rc $TIME_CMD -o $LOGDIR_RESULTS/dedup-${NR_THREADS}-$ITERATION.time \
		    $SHELLPACK_SOURCES/dedup-${VERSION}-installed/dedup \
			-v -c -p -t $NR_THREADS \
			-i $SHELLPACK_DATA/in.file  \
			-o $SHELLPACK_DATA/out.file \
				2>&1 | tee $LOGDIR_RESULTS/dedup-${NR_THREADS}-${ITERATION}.log
		recover_rc
		if [ $? -ne 0 ]; then
			die Failed to run dedup
		fi

		#cleanup
		rm $SHELLPACK_DATA/out.file
	###SHELLPACK iteration_end $ITERATIONS
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
###SHELLPACK threads_stride_end
rm -f $SHELLPACK_DATA/in.file $SHELLPACK_TEMP/in.file.gz
exit $SHELLPACK_SUCCESS
